package com.business.event;

import com.business.util.DerbyConn;
import com.business.util.TouchJdbc;
import com.xiaoleilu.hutool.util.NetUtil;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.sql.Connection;

/**
 * Created by mengdexuan on 2017/5/18 17:59.
 */
public class DealWithStartedEvent implements ApplicationListener<ContextRefreshedEvent> {

	private static org.slf4j.Logger log = LoggerFactory.getLogger(DealWithStartedEvent.class);



	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

		ApplicationContext appContext = contextRefreshedEvent.getApplicationContext();
		ServerProperties serverProperties = appContext.getBean(ServerProperties.class);

		TouchJdbc touchJdbc = TouchJdbc.getInstance();
		Connection conn = DerbyConn.getConn();
		try {
			String sql = "create table server_info(uuid varchar(64), ip varchar(16),port int,create_time TIMESTAMP,cluster_id  varchar(64))";
			touchJdbc.update(conn,sql);
			log.info("创建数据库表 ："+sql);
		}catch (Exception e){

		}
		try {
			String sql = "create table cluster_info(uuid varchar(64), name varchar(64),create_time TIMESTAMP)";
			touchJdbc.update(conn,sql);
			log.info("创建数据库表 ："+sql);
		}catch (Exception e){

		}


		StringBuffer sb = new StringBuffer();
		String ip = NetUtil.getLocalhostStr();
		sb.append("http://").append(ip).append(":").append(serverProperties.getPort()).append(serverProperties.getContextPath());


		log.info("项目启动完毕！");
		log.info("访问地址 --> "+sb.toString());
	}
}
